package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.dto.DeliveryAddressDto;
import com.dingreading.cloud.admin.entity.DeliveryAddress;
import com.dingreading.cloud.admin.service.DeliveryAddressService;
import com.dingreading.cloud.admin.util.bean.OrderFormV3Util;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/system/deliveryAddress")
@Api(tags = "总部-送货地址管理")
public class DeliveryAddressController {

    @Resource
    DeliveryAddressService deliveryAddressService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @RequestParam(value = "name", required = false) @ApiParam(value = "用户名/手机号") String name,
            @RequestParam(value = "status", required = false) @ApiParam(value = "默认状态 1.是 0.不是") Integer status
    ) {
        Page<DeliveryAddress> pageList = deliveryAddressService.pageList(pageUtil, null, null, name, status);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(pageList.getTotalRow());
        data.setList(pageList.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "获取地址详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        DeliveryAddress address = deliveryAddressService.getById(id);
        if (address == null) return R.fail("获取失败");

        DeliveryAddressDto addressDto = OrderFormV3Util.getAddressDto(address);

        return R.ok(addressDto);
    }


    @ApiOperation(value = "删除地址")
    @PostMapping(value = "/drop", produces = "application/json; charset=UTF-8")
    public R<Object> drop(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        DeliveryAddress address = deliveryAddressService.getById(id);
        if (address == null) return R.fail("获取失败");

        deliveryAddressService.deleteByStaffIdAndId(null, id);

        return R.ok();
    }

}
